// pages/login/login.js
function checkTarbar() {
    var tarbar = [
        'pages/index/index',
        'pages/order/order',
        'pages/personal/personal',
    ];
    for (var i in tarbar) {
        if (tarbar[i] == backRoute) {
            return true;
        } else {
            return false;
        }
    }
}
var app = getApp();
var util = require('../../utils/util');
var backRoute = '';
Page({

    /**
     * 页面的初始数据
     */
    data: {
		ispwd: false,
		username: "",
		password: ""
    },
    getBack: function() {
        var that = this;
        if (checkTarbar()) {
			console.log('/' + backRoute);
            wx.switchTab({
                url: '/' + backRoute,
            });
        } else {
			console.log('/' + backRoute,"sdjflkjkl");
            wx.reLaunch({
                url: '/' + backRoute,
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
		var that = this;
        backRoute = options.back_url != undefined ? options.back_url : 'pages/index/index';
		console.log(wx.getStorageSync('store_id').length);
		if(wx.getStorageSync('store_id').length > 1){
			that.getBack();
		}
    },
	//是否记住密码  使用
	onUserName: function (e) {
		this.setData({
			username: e.detail.value
		})
	},
	//是否记住密码  使用
	onPassWord: function (e) {
		this.setData({
			password: e.detail.value
		})
	},
	//是否记住密码
	pwdChecked: function (e) {
		this.data.ispwd = !this.data.ispwd;
		if (this.data.ispwd) {
			wx.setStorageSync("pwdInfo", {
				username: this.data.username,
				password: this.data.password
			})
		} else {
			wx.removeStorage({
				key: 'pwdInfo',
				success(res) {
					console.log(res)
				}
			})
		}
		this.setData({
			ispwd: this.data.ispwd
		})
		//   console.log(this.data.username, password);
	},
    formSubmit: function (e) {
        var that = this;
		// 查看是否授权
        var username = e.detail.value.username;
        if(username.length < 1){
            util.showSuccess('请输入账号','none',false);
            return false;
        }
        var password = e.detail.value.password;
        if(password.length < 1){
            util.showSuccess('请输入密码','none',false);
            return false;
        }
        // 登录
        wx.login({
            success: res => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                console.log(res);
                util.showLoading();
                var apiUrl = '/api/store/login/index_wx';
                var apiData = {
                    js_code:res.code,
                    username:username,
                    password:password
                }
                util.fetchPost(apiUrl,apiData,function(res){
                    util.hideLoading();
                    if(res.state == 1){
                        wx.setStorageSync('store_id',res.datas.id)
                        util.showSuccess('登录成功');
                        setTimeout(function(){
                            that.getBack();
                        },2000);
                    }else if(res.state == -3){
                        util.confirm('店铺账号未注册，是否注册',function(){
                            that.onRegisterTap();
                        });
                    }else{
                        util.message(res.datas);
                    }
                });
            }
        })
    },
    //跳转注册页面
    onRegisterTap:function(){
		wx.navigateTo({
			url: '../registerpage/registerpage',
		})
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})